<?php
/*
 * 车主金卡的数据库操作类
 * @package    Db
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 2d20c75752c47f5dbede9df0e91c1e20e6349037 $
 */
defined('IN_IXDCW') or exit('Access Denied');
final class OwncardDb extends CoreDb{


	public function __construct(){
		parent::__construct();
		$this->owncard_card_Name = $this->tableName().'_card';
		$this->owncard_buy_Name = $this->tableName().'_buy';
	}


	public function getFields(){
		return $this->fields(true);
	}


	/**
	 * 获取一条会员数据
	 * @param string $username
	 * @return array
	 */

	public function getByUsername($username) {
		$sql = "
			SELECT *
			FROM {$this->tableName()}
			WHERE username='".$username."'";
		return $this->_db->getOne($sql);
	}

	/**
	 * 添加会员数据
	 * @param array $user
	 * @return bool
	 */

	public function addUsername($user) {
		return $this->_db->query(Sql::insert($this->tableName(), $user, $this->getFields()));
	}

	/**
	 * 更新会员数据
	 * @param array $user
	 * @return bool
	 */

	public function updateUsername($user) {
		return $this->_db->query(Sql::update($this->tableName(), $user, "username ='".$user['username']."'",'',$this->getFields()));
	}

	/**
	 * 根据编号获取验证码
	 * @param array $number
	 * @return string
	 */

	public function getCodenum($number) {
		$sql = "
			SELECT cardcode
			FROM {$this->owncard_card_Name}
			WHERE cardnum='".$number."'";
		return $this->_db->getVar($sql);
	}

	/**
	 * 验证卡号是否使用
	 * @param array $cardnum
	 * @return string
	 */

	public function checkNum($cardnum) {
		$sql = "
			SELECT status
			FROM {$this->tableName()}
			WHERE cardnum='".$cardnum."'";
		return $this->_db->getVar($sql);
	}

	/**
	 * 更新订购信息
	 * @param array $post
	 * @return bool
	 */

	public function updateBuycard($post) {
		return $this->_db->query(Sql::update($this->owncard_buy_Name, $post, "itemid ='".$post['itemid']."'"));
	}

	/**
	 * 获取一条订购信息
	 * @param string $itemid
	 * @return array
	 */

	public function getByitem($itemid) {
		$sql = "
			SELECT *
			FROM {$this->owncard_buy_Name}
			WHERE itemid='".$itemid."'";
		return $this->_db->getOne($sql);
	}

	/**
	 * 添加订购信息
	 * @param array $post
	 * @return bool
	 */

	public function addBuycard($post) {
		global $db;
		$this->_db->query(Sql::insert($this->owncard_buy_Name, $post));
		return $db->insertId();
	}
}